
#include "aky_program/aky_com_include.h"
void func_check_pro(void);
static void my_event_cb(lv_obj_t * obj, lv_event_t event);
void fast_test_mode_ui_init(void);

static lv_obj_t *cb_low_bat;         //
static lv_obj_t *label_back;          //
static lv_obj_t *cb_speech_recognition;         //语音识别
static lv_obj_t *cb_4g;         //

static lv_obj_t *label_charge_stat;         //
static lv_obj_t *label_left_stat;         //
static lv_obj_t *label_right_stat;         //
static uint16_t step = 0;

/*
 * 界面回调函数
 */
static void screen015_on_timer(void *user_data)
{
    /* 界面定时器回调函数 */
    bat_flush_pro();
    func_check_pro();
}

static void screen015_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */
    step = 0;

    cb_low_bat = synwit_ui_find_lv_obj(WIDGET_CB2);
    cb_speech_recognition = synwit_ui_find_lv_obj(WIDGET_CB3);
    cb_4g = synwit_ui_find_lv_obj(WIDGET_CB1);
    label_back = synwit_ui_find_lv_obj(WIDGET_LABEL72);
    lv_obj_set_click(label_back, true);
    lv_obj_set_click(cb_low_bat, false);
    lv_obj_set_click(cb_speech_recognition, false);
    lv_obj_set_click(cb_4g, false);
    lv_checkbox_set_checked(cb_low_bat, false);
    lv_checkbox_set_checked(cb_speech_recognition, false);
    lv_checkbox_set_checked(cb_4g, false);


    voice_play_single(NORMAL);
    fast_test_mode_ui_init();

    lv_obj_set_event_cb(label_back, my_event_cb);   /*Assign an event callback*/

    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    synwit_ui_start_scr_timer(10, NULL);
}

static void screen015_stop(synwit_id_t screen_id)
{
    /* 准备切换到其它界面前，这个接口会被调用。 */
    aky_task_0_send_cmd(TASK0_BP_ENGINE_EXIT);
    aky_task_1_send_cmd(TASK1_BP_ENGINE_EXIT);
    aky_block_delay_ms(100);
    aky_task_0_send_cmd(TASK0_BP_ENGINE_EXIT);
    aky_task_1_send_cmd(TASK1_BP_ENGINE_EXIT);
    aky_block_delay_ms(100);
    aky_task_0_send_cmd(TASK0_BP_ENGINE_EXIT);
    aky_task_1_send_cmd(TASK1_BP_ENGINE_EXIT);


}

static int screen015_favorite_stop_policy()
{
    /* 函数返回值表示本界面结束时，希望采用的资源管理策略 */

    /* 资源管理策略(主要影响本界面再次进入时的加载速度):
     *    0: 系统推荐，释放当前界面引用的资源，保留控件及控件状态。可获得平衡的界面切换性能和内存消耗
     *    1: 释放当前界面引用的资源，并删除所有控件。
     *    2: 保留当前界面的所有资源及控件。可以获得最佳的界面切换性能，但内存消耗较大
     */
    return 0;
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen015_cb_obj = {
    .favorite_stop_policy = screen015_favorite_stop_policy,
    .on_screen_start = screen015_start,
    .on_screen_stop = screen015_stop,
    .on_screen_timer = screen015_on_timer,
};


static void my_event_cb(lv_obj_t * obj, lv_event_t event)
{
    if (obj == label_back)
    {
        if (event == LV_EVENT_CLICKED)
        {
            {
                synwit_ui_load_screen(SCREEN012);
            }
        }
    }

}
void func_check_pro(void)
{
    static uint16_t fil_cnt;
    static uint32_t u32_tick_last = 0;
    static uint8_t flag_low_bat = 0;
    static uint8_t check_onece = 0;
    static uint16_t step2 = 0;
    static uint16_t send_cnt = 0;
    static uint16_t step3 = 0;
    static uint16_t send_cnt2 = 0;
    static uint32_t u32_tick_last3 = 0;
    static uint32_t u32_tick_last2 = 0;

    static e_charge_stat_t e_charge_stat_last = 0xff;
    off_time_clr();
    
    if (step == 0)
    {
        step++;
        check_onece = 0;
        flag_low_bat = 0;
        step2 = 0;
        send_cnt = 0;
        step3 = 0;
        send_cnt2 = 0;
    }
    else if (step == 1)
    {
        if (step2 == 0)
        {
            step2++;
            lv_label_set_text_fmt(label_left_stat,"左侧血压状态=");
        }
        else if (step2 == 1)
        {
            if (g_u32_tick_cnt - u32_tick_last2 > 300 / 10)
            {
                u32_tick_last2 = g_u32_tick_cnt;
                aky_task_0_send_cmd(TASK0_FAST_TEST_MODE);
                send_cnt++;
                if (send_cnt >= 20)
                {
                    send_cnt = 0;
                    lv_label_set_text_fmt(label_left_stat,"左侧血压状态=通信失败");
                    step2 = 2;
                }
                if (Task0_BP.e_aky_task_0_stat == TASK_0_STAT_FAST_TEST)
                {
                    lv_label_set_text_fmt(label_left_stat,"左侧血压状态=通信成功 快速测试");
                    step2 = 2;
                }
    
            }

        }


        if (step3 == 0)
        {
            step3++;
            lv_label_set_text_fmt(label_right_stat,"右侧血压状态=");
        }
        else if (step3 == 1)
        {
            if (g_u32_tick_cnt - u32_tick_last3 > 300 / 10)
            {
                u32_tick_last3 = g_u32_tick_cnt;
                aky_task_1_send_cmd(TASK1_FAST_TEST_MODE);
                send_cnt2++;
                if (send_cnt2 >= 20)
                {
                    send_cnt2 = 0;
                    lv_label_set_text_fmt(label_right_stat,"右侧血压状态=通信失败");
                    step3 = 2;
                }
                if (Task1_BP.e_aky_task_1_stat == TASK_1_STAT_FAST_TEST)
                {
                    lv_label_set_text_fmt(label_right_stat,"右侧血压状态=通信成功 快速测试");
                    step3 = 2;
                }
    
            }
        }



        if (e_charge_stat_last != get_cur_charge_stat())
        {
            e_charge_stat_last = get_cur_charge_stat();
            if (e_charge_stat_last == NO_CHARGE)
            {
                lv_label_set_text_fmt(label_charge_stat,"充电状态=无");
            }
            else if (e_charge_stat_last == FULL_BAT)
            {
                lv_label_set_text_fmt(label_charge_stat,"充电状态=已充满");
            }
            else
            {
                lv_label_set_text_fmt(label_charge_stat,"充电状态=正在充电");
            }
        }
        


        if(check_onece == 0)
        {
            if (g_u32_tick_cnt - u32_tick_last > 500 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                send_4g_test_cmd();
            }
            if (get_4g_test_ok_flag() == 1)
            {
                lv_checkbox_set_checked(cb_4g, true);
                check_onece = 1;
            }
        }

        
        if (check_low_bat_trig(LOW_BAT_THRESHOLD) == 1)
        {
            flag_low_bat = 1;
        }
    
        if (flag_low_bat == 1)
        {
            flag_low_bat = 0;
            lv_checkbox_set_checked(cb_low_bat, true);
            if (get_voice_busy_stat() == 0)
            {
                voice_play_single(VIOCE_LOW_BAT);// 电力不足，请充电
            }    
        }
        if (get_spk_recogni_info() == SPK_WAKE)
        {
            clr_spk_recogni_info();
            lv_checkbox_set_checked(cb_speech_recognition, true);
        }
    
    }

}

void fast_test_mode_ui_init(void)
{
    static uint8_t flag_init_onece = 0;
    if (flag_init_onece == 0)
    {
        flag_init_onece = 1;
    }
    else if (flag_init_onece == 1)
    {
        return;
    }
    
    lv_font_t* font_txt = synwit_ui_font_load("S:spi:wqyMicroHeiMono_40.hmf", false);

    label_charge_stat = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_long_mode(label_charge_stat, LV_LABEL_LONG_BREAK);
    lv_obj_set_style_local_text_font(label_charge_stat, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, font_txt);	
    lv_obj_set_size(label_charge_stat, 500, 50);
    lv_obj_set_pos(label_charge_stat, 0, 100);
    lv_label_set_text_fmt(label_charge_stat,"充电状态=无");


    label_left_stat = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_long_mode(label_left_stat, LV_LABEL_LONG_BREAK);
    lv_obj_set_style_local_text_font(label_left_stat, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, font_txt);	
    lv_obj_set_size(label_left_stat, 800, 50);
    lv_obj_set_pos(label_left_stat, 0, 150);
    lv_label_set_text_fmt(label_left_stat,"左侧血压状态=");

    label_right_stat = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_long_mode(label_right_stat, LV_LABEL_LONG_BREAK);
    lv_obj_set_style_local_text_font(label_right_stat, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, font_txt);	
    lv_obj_set_size(label_right_stat, 800, 50);
    lv_obj_set_pos(label_right_stat, 0, 200);
    lv_label_set_text_fmt(label_right_stat,"右侧血压状态=");

}


